<?php

namespace App\Http\Controllers;

use App\Constants\AdminRoleConstant;
use App\Constants\ResponseCodeConstant;
use App\Http\Requests\RoleCreateRequest;
use App\Http\Requests\RoleStatusRequest;
use App\Services\AdminRoleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

/**
 * 后台角色管理
 * Class AdminUserController
 * @package App\Http\Controllers
 */
class AdminRoleController extends BaseController
{
    /**
     * 创建角色
     * @param RoleCreateRequest $request
     * @return JsonResponse
     */
    public function create(RoleCreateRequest $request): JsonResponse
    {
        $name = $request->post('name');
        $description = $request->post('description');
        $data = [
            'name' => $name,
            'description' => $description,
            'status' => AdminRoleConstant::STATUS_ON,
            'sort' => 0,
            'admin_count' => 0
        ];
        $result = AdminRoleService::getInstance()->create($data);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::CREATE_FAILED,'角色创建失败');
    }

    /**
     * 根据ID修改角色
     * @param Request $request
     * @return JsonResponse|mixed|object
     * @throws ValidationException
     */
    public function update(Request $request)
    {
        $id = $request->route('id');
        $name = $request->post('name');
        $description = $request->post('description');
        $status = $request->post('status');
        $sort = $request->post('sort');
        $data = [];
        if ($request->has('name')) {
            $this->validate($request, ['name' => ['unique:admin_roles,name,' . $id]],['name.unique' => '角色名称已存在']);
            $data['name'] = $name;
        }
        if ($request->has('description')) {
            $data['description'] = $description;
        }
        if ($request->has('status')) {
            $data['status'] = $status;
        }
        if ($request->has('sort')) {
            $data['sort'] = $sort;
        }
        $result = false;
        if($data) {
            $result = AdminRoleService::getInstance()->updateById($data,$id);
        }
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'角色修改失败');
    }

    /**
     * 根据ID删除角色
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function delete(Request $request)
    {
        $id = $request->route('id');
        $result = AdminRoleService::getInstance()->deleteById($id);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::DELETE_FAILED,'角色删除失败');
    }

    /**
     *  批量删除角色
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function deleteBatch(Request $request)
    {
        $ids = $request->post('ids');
        $result = AdminRoleService::getInstance()->deleteBatchByIds($ids);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::DELETE_FAILED,'角色删除失败');
    }

    /**
     * 获取所有角色
     * @return mixed
     */
    public function listAll()
    {
        $result = AdminRoleService::getInstance()->listAll();
        return $this->success($result);
    }

    /**
     * 分页获取角色列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->get('keyword');
        $pageSize = $request->get('pageSize',15);
        $pageNum = $request->get('pageNum');
        $data = AdminRoleService::getInstance()->list($keyword,$pageSize,$pageNum);
        $result = $this->successPagniate($data);
        return $this->success($result);
    }

    /**
     * 修改角色状态
     * @param RoleStatusRequest $request
     * @return JsonResponse|mixed|object
     */
    public function updateStatus(RoleStatusRequest $request)
    {
        $status = $request->post('status');
        $id = $request->route('id');
        $result = AdminRoleService::getInstance()->updateStatusById($status,$id);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'角色状态修改失败');
    }

    /**
     * 获取角色相关菜单
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function listMenu(Request $request)
    {
        $roleId = $request->route('roleId');
        $result = AdminRoleService::getInstance()->getMenuById($roleId);
        if ($request) {
            return $this->success($result);
        }
        return $this->fail(ResponseCodeConstant::SEARCH_FAILED,'角色菜单查询失败');
    }

    /**
     * 获取角色相关资源
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function listResource(Request $request)
    {
        $roleId = $request->route('roleId');
        $result = AdminRoleService::getInstance()->getResourceById($roleId);
        if ($request) {
            return $this->success($result);
        }
        return $this->fail(ResponseCodeConstant::SEARCH_FAILED,'角色资源查询失败');
    }

    /**
     * 给角色分配菜单
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function allocMenu(Request $request)
    {
        $roleId = $request->post('roleId');
        $menuIds = $request->post('menuIds');
        $result = AdminRoleService::getInstance()->allocMenu($roleId,$menuIds);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'角色菜单分配失败');
    }

    /**
     * 给角色分配资源
     * @param Request $request
     * @return JsonResponse|mixed|object
     */
    public function allocResource(Request $request)
    {
        $roleId = $request->post('roleId');
        $resourceIds = $request->post('resourceIds');
        $result = AdminRoleService::getInstance()->allocMenu($roleId,$resourceIds);
        if ($result) {
            return $this->success();
        }
        return $this->fail(ResponseCodeConstant::UPDATE_FAILED,'角色资源分配失败');
    }
}
